﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

using System.Web.Http;
using OF.AppContext;
using OF.Utility;

namespace CWQ.Api.Filters
{
    /// <summary>
    /// 用户ID过滤器
    /// </summary>
    public class UserIDFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var request = actionContext.Request;
            int userId = EConvert.ToInt(GetHeaderValue(actionContext,"userid"),0);
            UserContext.Current = new UserContext()
            {
                UserID = userId
            };

            base.OnActionExecuting(actionContext);
        }
        public string GetHeaderValue(HttpActionContext actionContext, string name)
        {
            IEnumerable<string> headers;
            if (actionContext.Request.Headers.TryGetValues(name, out headers))
            {
                return headers.FirstOrDefault();
            }
            return "";
        }
    }
}